package com.lza.phone.service.impl;

import com.lza.phone.entity.User;
import com.lza.phone.mapper.UserMapper;
import com.lza.phone.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lza.redis.RedisUtil;
import com.lza.response.Result;
import com.lza.security.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李钊澳
 * @since 2021-09-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private RedisUtil redisUtil;//redis工具类

    @Override
    public Result login(String username, String password, HttpServletRequest httpServletRequest) {
        //登录
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if(null==userDetails){
            return Result.error().getMessage("用户不正确");
        }if(!passwordEncoder.matches(password, userDetails.getPassword())){
            return Result.error().getMessage("密码不正确");
        }
//        if(!userDetails.isEnabled()){
//            return  Result.error().getMessage("账号被禁用，请联系管理员");
//        }
        //更新security对象
        UsernamePasswordAuthenticationToken authenticationToken=new
                UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        Map<String,Object> tokenMap=new HashMap<>();
       /* //把token放到redis上
        redisUtil.set("token", token);
        //把token的有效期设置为一天
        redisUtil.expire("token", 1, TimeUnit.DAYS);
        redisUtil.set("token", token);*/
        tokenMap.put("tokenHeader",tokenHead);
        System.out.println("根据用户名和密码生成的token为"+token);
        String userNameFromToken = jwtTokenUtil.getUserNameFromToken(token);
        System.out.println("根据token生成用户名"+userNameFromToken);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();//信息保存到了SecurityContextHolder.getContext().setAuthentication(authResult);
        System.out.println("信息保存在改地方:"+authentication);
        return Result.ok().getMessage("登录成功").getData("token:",token);
    }
}
